package org.micro.rbac.util;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.IFill;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.fill.Column;
import com.google.common.collect.Lists;

import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * mybatis-plus 代码生成器
 *
 * @author hongsir
 * @since 2024/6/7
 **/
public class GeneratorUtil {

    public static void main(String[] args) {
        List<String> tables = Lists.newArrayList("sys_log","sys_user","sys_role","sys_resource","sys_role_resource","sys_user_role");
        List<IFill> fills = new ArrayList<>(4);
        fills.add(new Column("create_time", FieldFill.INSERT));
        fills.add(new Column("create_user", FieldFill.INSERT));
        fills.add(new Column("update_time", FieldFill.UPDATE));
        fills.add(new Column("update_user", FieldFill.UPDATE));
        FastAutoGenerator.create("jdbc:h2:file:D:/Data/H2/rbac", "sa", "")
                .globalConfig(builder -> builder
                        .author("HongSir")
                        .enableSpringdoc()
                        .disableOpenDir()
                        .outputDir(Paths.get(System.getProperty("user.dir")) + "/src/main/java")
                        .commentDate("yyyy-MM-dd")
                )
                .packageConfig(builder -> builder
                        .parent("org.micro.rbac")
                        .entity("dao.entity")
                        .mapper("dao.mapper")
                        .service("service")
                        .controller("controller")
                        .serviceImpl("service.impl")
                        //.xml("mapper.xml")
                        .pathInfo(Collections.singletonMap(OutputFile.xml,
                                Paths.get(System.getProperty("user.dir")) + "/src/main/resources/mapper")
                        )
                )
                .strategyConfig(builder -> {
                    builder.addInclude(tables)
                            .build()
                            .entityBuilder()
                            .enableFileOverride()
                            .enableTableFieldAnnotation()
                            .enableLombok()
                            .addTableFills(fills)
                            .serviceBuilder()
                            .enableFileOverride()
                            .controllerBuilder()
                            .enableFileOverride()
                            .enableRestStyle()
                            .enableHyphenStyle()
                            .mapperBuilder()
                            .enableFileOverride()
                    ;
                })
                .templateEngine(new FreemarkerTemplateEngine())
                .execute();
    }

}
